define(function (require, exports, module) {

    require("jqtree");

    var $ = require("jquery");

    var angular = require("angular");

    var _ = require('underscore');

    angular.module('ykTree', [])
        .directive('ykTree', function () {
            return {
                require: ['ykTree'],
                restrict: 'AE',
                scope: {
                    "data": "=ykTreeData",
                    "value": "=ngModel",
                    'config': '=ykTreeConfig'
                },
                controller: ['$scope', function ($scope) {

                    var $tree, defaultConfig = {
                        dragAndDrop: false,
                        useContextMenu: false,
                        selectable: true,
                        autoOpen: 1,
                        onCreateLi: function () {
                        },
                        onCanMove: function () {//是否可以移动
                            return false;
                        },
                        onCanMoveTo: function () {//是否可以移动到
                            return false;
                        },
                        onIsMoveHandle: function () {//移动句柄
                            return false;
                        },
                        onCanSelectNode: function () {
                            return true;
                        }
                    };

                    this.createTree = function ($tree) {
                        this.setTree($tree);
                        this.applyConfig();
                        $tree.tree(defaultConfig)
                            .on('tree.click', defaultConfig.treeClick || angular.noop);
                    };

                    this.setTree = function (tree) {
                        $tree = tree;
                    };

                    this.setTreeData = function (data) {
                        $tree.tree('loadData', this.transformData(data));
                    };

                    this.transformData = function (data) {
                        return data;
                    };

                    this.applyConfig = function () {
                        if ($scope.config) {
                            angular.extend(defaultConfig, $scope.config);
                        }
                    };

                    this.init = function ($tree) {

                        var self = this;

                        if ($scope.config.transformData) {
                            self.transformData = $scope.config.transformData;
                        }

                        self.createTree($tree);

                        $scope.$watch("data", function (newValue) {

                            if (newValue) {
                                self.setTreeData(newValue);
                            } else {
                                // nothing
                            }

                        });

                    }

                }],
                link: function (scope, element, attrs, ctrls) {

                    scope.config = scope.config || {};

                    ctrls[0].init($(element[0]));

                }
            }
        });
});